package http

// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.

//go:generate minimock -i github.com/deckhouse/deckhouse/go_lib/dependency/http.Client -o ./http_mock.go

import (
	"net/http"
	"sync"
	mm_atomic "sync/atomic"
	mm_time "time"

	"github.com/gojuno/minimock/v3"
)

// ClientMock implements Client
type ClientMock struct {
	t minimock.Tester

	funcDo          func(req *http.Request) (rp1 *http.Response, err error)
	inspectFuncDo   func(req *http.Request)
	afterDoCounter  uint64
	beforeDoCounter uint64
	DoMock          mClientMockDo
}

// NewClientMock returns a mock for Client
func NewClientMock(t minimock.Tester) *ClientMock {
	m := &ClientMock{t: t}
	if controller, ok := t.(minimock.MockController); ok {
		controller.RegisterMocker(m)
	}

	m.DoMock = mClientMockDo{mock: m}
	m.DoMock.callArgs = []*ClientMockDoParams{}

	return m
}

type mClientMockDo struct {
	mock               *ClientMock
	defaultExpectation *ClientMockDoExpectation
	expectations       []*ClientMockDoExpectation

	callArgs []*ClientMockDoParams
	mutex    sync.RWMutex
}

// ClientMockDoExpectation specifies expectation struct of the Client.Do
type ClientMockDoExpectation struct {
	mock    *ClientMock
	params  *ClientMockDoParams
	results *ClientMockDoResults
	Counter uint64
}

// ClientMockDoParams contains parameters of the Client.Do
type ClientMockDoParams struct {
	req *http.Request
}

// ClientMockDoResults contains results of the Client.Do
type ClientMockDoResults struct {
	rp1 *http.Response
	err error
}

// Expect sets up expected params for Client.Do
func (mmDo *mClientMockDo) Expect(req *http.Request) *mClientMockDo {
	if mmDo.mock.funcDo != nil {
		mmDo.mock.t.Fatalf("ClientMock.Do mock is already set by Set")
	}

	if mmDo.defaultExpectation == nil {
		mmDo.defaultExpectation = &ClientMockDoExpectation{}
	}

	mmDo.defaultExpectation.params = &ClientMockDoParams{req}
	for _, e := range mmDo.expectations {
		if minimock.Equal(e.params, mmDo.defaultExpectation.params) {
			mmDo.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmDo.defaultExpectation.params)
		}
	}

	return mmDo
}

// Inspect accepts an inspector function that has same arguments as the Client.Do
func (mmDo *mClientMockDo) Inspect(f func(req *http.Request)) *mClientMockDo {
	if mmDo.mock.inspectFuncDo != nil {
		mmDo.mock.t.Fatalf("Inspect function is already set for ClientMock.Do")
	}

	mmDo.mock.inspectFuncDo = f

	return mmDo
}

// Return sets up results that will be returned by Client.Do
func (mmDo *mClientMockDo) Return(rp1 *http.Response, err error) *ClientMock {
	if mmDo.mock.funcDo != nil {
		mmDo.mock.t.Fatalf("ClientMock.Do mock is already set by Set")
	}

	if mmDo.defaultExpectation == nil {
		mmDo.defaultExpectation = &ClientMockDoExpectation{mock: mmDo.mock}
	}
	mmDo.defaultExpectation.results = &ClientMockDoResults{rp1, err}
	return mmDo.mock
}

// Set uses given function f to mock the Client.Do method
func (mmDo *mClientMockDo) Set(f func(req *http.Request) (rp1 *http.Response, err error)) *ClientMock {
	if mmDo.defaultExpectation != nil {
		mmDo.mock.t.Fatalf("Default expectation is already set for the Client.Do method")
	}

	if len(mmDo.expectations) > 0 {
		mmDo.mock.t.Fatalf("Some expectations are already set for the Client.Do method")
	}

	mmDo.mock.funcDo = f
	return mmDo.mock
}

// When sets expectation for the Client.Do which will trigger the result defined by the following
// Then helper
func (mmDo *mClientMockDo) When(req *http.Request) *ClientMockDoExpectation {
	if mmDo.mock.funcDo != nil {
		mmDo.mock.t.Fatalf("ClientMock.Do mock is already set by Set")
	}

	expectation := &ClientMockDoExpectation{
		mock:   mmDo.mock,
		params: &ClientMockDoParams{req},
	}
	mmDo.expectations = append(mmDo.expectations, expectation)
	return expectation
}

// Then sets up Client.Do return parameters for the expectation previously defined by the When method
func (e *ClientMockDoExpectation) Then(rp1 *http.Response, err error) *ClientMock {
	e.results = &ClientMockDoResults{rp1, err}
	return e.mock
}

// Do implements Client
func (mmDo *ClientMock) Do(req *http.Request) (rp1 *http.Response, err error) {
	mm_atomic.AddUint64(&mmDo.beforeDoCounter, 1)
	defer mm_atomic.AddUint64(&mmDo.afterDoCounter, 1)

	if mmDo.inspectFuncDo != nil {
		mmDo.inspectFuncDo(req)
	}

	mm_params := &ClientMockDoParams{req}

	// Record call args
	mmDo.DoMock.mutex.Lock()
	mmDo.DoMock.callArgs = append(mmDo.DoMock.callArgs, mm_params)
	mmDo.DoMock.mutex.Unlock()

	for _, e := range mmDo.DoMock.expectations {
		if minimock.Equal(e.params, mm_params) {
			mm_atomic.AddUint64(&e.Counter, 1)
			return e.results.rp1, e.results.err
		}
	}

	if mmDo.DoMock.defaultExpectation != nil {
		mm_atomic.AddUint64(&mmDo.DoMock.defaultExpectation.Counter, 1)
		mm_want := mmDo.DoMock.defaultExpectation.params
		mm_got := ClientMockDoParams{req}
		if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
			mmDo.t.Errorf("ClientMock.Do got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
		}

		mm_results := mmDo.DoMock.defaultExpectation.results
		if mm_results == nil {
			mmDo.t.Fatal("No results are set for the ClientMock.Do")
		}
		return (*mm_results).rp1, (*mm_results).err
	}
	if mmDo.funcDo != nil {
		return mmDo.funcDo(req)
	}
	mmDo.t.Fatalf("Unexpected call to ClientMock.Do. %v", req)
	return
}

// DoAfterCounter returns a count of finished ClientMock.Do invocations
func (mmDo *ClientMock) DoAfterCounter() uint64 {
	return mm_atomic.LoadUint64(&mmDo.afterDoCounter)
}

// DoBeforeCounter returns a count of ClientMock.Do invocations
func (mmDo *ClientMock) DoBeforeCounter() uint64 {
	return mm_atomic.LoadUint64(&mmDo.beforeDoCounter)
}

// Calls returns a list of arguments used in each call to ClientMock.Do.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmDo *mClientMockDo) Calls() []*ClientMockDoParams {
	mmDo.mutex.RLock()

	argCopy := make([]*ClientMockDoParams, len(mmDo.callArgs))
	copy(argCopy, mmDo.callArgs)

	mmDo.mutex.RUnlock()

	return argCopy
}

// MinimockDoDone returns true if the count of the Do invocations corresponds
// the number of defined expectations
func (m *ClientMock) MinimockDoDone() bool {
	for _, e := range m.DoMock.expectations {
		if mm_atomic.LoadUint64(&e.Counter) < 1 {
			return false
		}
	}

	// if default expectation was set then invocations count should be greater than zero
	if m.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
		return false
	}
	// if func was set then invocations count should be greater than zero
	if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
		return false
	}
	return true
}

// MinimockDoInspect logs each unmet expectation
func (m *ClientMock) MinimockDoInspect() {
	for _, e := range m.DoMock.expectations {
		if mm_atomic.LoadUint64(&e.Counter) < 1 {
			m.t.Errorf("Expected call to ClientMock.Do with params: %#v", *e.params)
		}
	}

	// if default expectation was set then invocations count should be greater than zero
	if m.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
		if m.DoMock.defaultExpectation.params == nil {
			m.t.Error("Expected call to ClientMock.Do")
		} else {
			m.t.Errorf("Expected call to ClientMock.Do with params: %#v", *m.DoMock.defaultExpectation.params)
		}
	}
	// if func was set then invocations count should be greater than zero
	if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
		m.t.Error("Expected call to ClientMock.Do")
	}
}

// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *ClientMock) MinimockFinish() {
	if !m.minimockDone() {
		m.MinimockDoInspect()
		m.t.FailNow()
	}
}

// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *ClientMock) MinimockWait(timeout mm_time.Duration) {
	timeoutCh := mm_time.After(timeout)
	for {
		if m.minimockDone() {
			return
		}
		select {
		case <-timeoutCh:
			m.MinimockFinish()
			return
		case <-mm_time.After(10 * mm_time.Millisecond):
		}
	}
}

func (m *ClientMock) minimockDone() bool {
	done := true
	return done &&
		m.MinimockDoDone()
}
